﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Techstar.Dialog;
using SmartMD.DataAccess.Service;
using Techstar.Progress;

namespace SmartMD.Manager.SysInfo
{
    /// <summary>
    /// Interaction logic for ExamItemListPad.xaml
    /// </summary>
    public partial class ExamItemListPad : BasePad
    {
        private ObservableCollection<ExamItemInfo> _examItemInfos;
        public ObservableCollection<ExamItemInfo> examItemInfos 
        {
            get 
            {
                return _examItemInfos;
            }
            set 
            {
                _examItemInfos = value;
            }
        }

        public ExamItemListPad()
        {
            InitializeComponent();
            //初始化数据
            ObservableCollection<ExamItemInfo> items;
            ProgressThread progressThread = new ProgressThread(200);
            try
            {
                progressThread.Start("正在加载数据", "正在加载检查项目......");
                progressThread.Step = "正在加载检查项目......";
                items = DataServiceManager.ExamItemService.loadAllExamItemInfo();
            }
            finally
            {
                progressThread.End();
            }
       
            if (items != null)
            {
                examItemInfos = items;
            }
            else
            {
                examItemInfos = new ObservableCollection<ExamItemInfo>();
            }
            ICollectionView view = CollectionViewSource.GetDefaultView(examItemInfos);

            ExamItemListView.ItemsSource = view;
        }

        private static ExamItemListPad _instance;
        public static ExamItemListPad Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ExamItemListPad();
                    MainWindow.AddLeftPad(_instance);
                }
               
                return _instance;
            }
        }
        public ExamItemMaintain Owner { get; set; }

        public void AddItem(ExamItemInfo item)
        {
            examItemInfos.Add(item);
        }

        private void BtnSearch_Click(object sender, RoutedEventArgs e)
        {

        }

        private void TxtSearch_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void ExamItemList_PreviewMouseMove(object sender, MouseEventArgs e)
        {

        }

        private void ExamItemList_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));
            bool isItemSelected = (item != null);
            if (isItemSelected)
            {
                ContextMenu ctxMenu = new ContextMenu();
                MenuItem mItem = new MenuItem();
                mItem.Width = 160;
                mItem.Height = 20;
                mItem.Header = "新建(N)";
                Image img = new Image();
                ImageSource imageSource = new BitmapImage(new Uri("/Images/Toolbar/16x16/new.png", UriKind.RelativeOrAbsolute));
                img.Source = imageSource;
                mItem.Icon = img;
                ctxMenu.Items.Add(mItem);
                mItem = new MenuItem();
                mItem.Header = "保存(S)";
                img = new Image();
                imageSource = new BitmapImage(new Uri("/Images/Toolbar/16x16/save.png", UriKind.RelativeOrAbsolute));
                img.Source = imageSource;
                mItem.Icon = img;
                ctxMenu.Items.Add(mItem);
                mItem = new MenuItem();
                mItem.Header = "删除(D)";
               
                img = new Image();
                imageSource = new BitmapImage(new Uri("/Images/Toolbar/16x16/delete.png", UriKind.RelativeOrAbsolute));
                img.Source = imageSource;
                mItem.Icon = img;
                ctxMenu.Items.Add(mItem);
                this.ExamItemListView.ContextMenu = ctxMenu;
            }
            else
            {
                this.ExamItemListView.ContextMenu = null;
            }
        }
        private object GetElementFromPoint(ItemsControl itemsControl, Point point)
        {
            UIElement element = itemsControl.InputHitTest(point) as UIElement;
            while (element != null)
            {
                if (element == itemsControl)
                    return null;
                object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element);
                if (!item.Equals(DependencyProperty.UnsetValue))
                    return item;
                element = (UIElement)VisualTreeHelper.GetParent(element);
            }
            return null;
        }

        public void DeleteSelectedItem(ExamItemInfo item=null)
        {
            if (item == null)//设置删除标志
            {
                ExamItemInfo selectedItem = (ExamItemInfo)this.ExamItemListView.SelectedItem;
                if (selectedItem == null)
                {
                    TaskDialogEx.Information("请选择删除项！");
                    return;
                }
                else
                {
                   
                    selectedItem.State = DataModel.RecordState.Deleted;
                }
            }
            else //删除元素
            {
                for (int i = 0; i < examItemInfos.Count; i++)
                {
                    if (item.Id == examItemInfos[i].Id)
                    {
                        this.examItemInfos.Remove(examItemInfos[i]);
                    }                
                }
                this.Owner.ucExamItemDefine.DataSource = null;
                    
            }
            
        }

        public void EditSelectedItem()
        {
            ExamItemInfo selectedItem = (ExamItemInfo)this.ExamItemListView.SelectedItem;
            if (selectedItem == null)
            {
                TaskDialogEx.Information("请选择编辑项！");
                return;
            }
            else
            {               
                selectedItem.State = DataModel.RecordState.Modified;
                selectedItem.LastModify = System.DateTime.Now;
            }
        }

        private void ExamItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ExamItemListView.SelectedItem == null)
                return;
            this.Owner.ucExamItemDefine.IsEnabled = true;
            this.Owner.ucExamItemDefine.DataSource = (ExamItemInfo)this.ExamItemListView.SelectedItem;
        }
    }
}
